#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <time.h>

#define UTMP_LEN sizeof(struct utmp)
#define TIME_ZONE 8
char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

void show_time(time_t sec)
{
	struct tm *p;
	sec = sec+18*60*60;
	p=gmtime(&sec);

	printf("%d/%d/%d ", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
	printf("%s %d:%d:%d", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}

void show_info(struct utmp *p_utmp)
{

	printf("% -18.18s", p_utmp->ut_name);
	printf(" ");
	printf("% -8.8s", p_utmp->ut_line);
	printf(" ");
	show_time(p_utmp->ut_time);
	printf(" ");
	printf("(%s)", p_utmp->ut_host);
	printf(" \n");
}


int main(int argc, char **argv)
{
	struct utmp login_record;
	int utmp_fd;
	utmp_fd = open("/var/run/utmp", O_RDONLY);
	if(utmp_fd==-1)
		exit(1);


	while(UTMP_LEN==read(utmp_fd, (void*)&login_record, UTMP_LEN))
	{
		if(login_record.ut_type==USER_PROCESS)
			show_info(&login_record);
	}
	close(utmp_fd);
	return 0;
}

